package fr.contacts.controllers;

import org.datanucleus.store.types.sco.simple.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import fr.contacts.constant.Constant;
import fr.contacts.models.Adresse;
import fr.contacts.models.Contact;

import java.text.ParseException;
import java.util.Calendar;
import java.util.List;

@SessionAttributes({"contact", "adresse"})
@Controller
public class EditContact {
	
	@RequestMapping(value = "/contact/edit",  method=RequestMethod.GET)
	public String edit(
			@RequestParam("id") int id,
			Model model ) throws ParseException {
		Constant.clearErrorList();
		
		System.out.println("EditController: Passing through... ");
		
		Contact contact = ContactsDAO.readContact(id);
		System.out.println("Edit Contact : " + contact);
		if( contact == null ) {
			Constant.addError("Le contact n°" + id + " n'existe pas !");
			return "/WEB-INF/views/list.jsp";
		}
		
		List<Adresse> adresses = ContactsDAO.readAllAdresse(id);
		
		model.addAttribute("contact", contact);
		model.addAttribute("adresses", adresses);
		model.addAttribute("user_action", "edit");

		Calendar cal = Calendar.getInstance();
		cal.setTime(contact.getDate());
		model.addAttribute("dayParam", String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
		model.addAttribute("monthParam", convertMonth(cal.get(Calendar.MONTH) + 1));
		model.addAttribute("yearParam", cal.get(Calendar.YEAR));
		
		
		
		return "/WEB-INF/views/create.jsp";
	}
		
	@RequestMapping(value = "/contact/adresse/edit" )
	public String editAdresse(@ModelAttribute("contact") Contact contact,
			 				  @RequestParam("id") int idContact,
			 				  @RequestParam("idAdresse") int idAdresse,
			 				  Model model ) throws ParseException {
		
		System.out.println("EditController ADRESSE: Passing through... ");
		contact = ContactsDAO.readContact(idContact);
		System.out.println("Edit Contact : " + contact);
		if( contact == null ) {
			Constant.addError("Le contact n°" + idContact + " n'existe pas !");
			return "redirect:/list";
		}
		
		Adresse adresse = ContactsDAO.readAdresse(idContact, idAdresse);
		model.addAttribute("contact", contact);
		model.addAttribute("adresse", adresse);
		model.addAttribute("user_action", "update");
		model.addAttribute("errors", Constant.errorList);
				
		return "/WEB-INF/views/adresseform.jsp";
	}
	
	
	private String convertMonth(int monthParam) {
		Constant.fillDateIntToStringMap();
    	return Constant.dateIntToStringMap.get(monthParam);
    }
	
	@RequestMapping(value = "/contact/delete",  method=RequestMethod.GET)
	public String deleteContact(@RequestParam("id") int id) {
		ContactsDAO.deleteContact(id);
		return "redirect:/list";
	}
	
	@RequestMapping(value = "/contact/adresse/delete",  method=RequestMethod.GET)
	public String deleteAdresse(@RequestParam("id") int id, @RequestParam("idAdresse") int idAdresse) {
		
		ContactsDAO.deleteAdresse(id, idAdresse);
		return "redirect:/contact?id=" + id;
	}
	
	
	

}
